<template>
  <div
    v-loading="loading"
    class="myCollects"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
  >
    <el-empty v-if="postList.length === 0" description="暂无相关帖子" />
    <post
      v-for="item in postList"
      v-else
      :key="item.id"
      class="post"
      :post-info="item"
      :like="handleLike"
      :dislike="handleDislike"
      :collect="handleCollect"
      :report="handleReport"
      @click.native="goPostDetail(item)"
    />
  </div>
</template>

<script>
import post from '@/components/post'
import { savePostFeedback, updatePostFeedback } from '@/api/postfeedback'
import { getPostVoList } from '@/api/post'
import { mapGetters } from 'vuex'
import pubsub from 'pubsub-js'
export default {
  name: 'MyCollects',
  components: {
    post
  },
  data() {
    return {
      postList: [],
      pageNum: 1,
      pageSize: 5,
      pubsubId: undefined,
      loading: false
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  beforeMount() {
    this.getPostVoList()
    this.subscribeUpdatePostVoList()
  },
  beforeDestroy() {
    pubsub.unsubscribe(this.pubsubId)
  },
  methods: {
    subscribeUpdatePostVoList() {
      this.pubsubId = pubsub.subscribe('updateData', () => {
        this.pageSize += 5
        this.getPostVoList()
      })
    },
    async getPostVoList() {
      this.loading = true
      const searchParam = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        searches: [
          {
            key: 'post.user_id',
            rule: '=',
            value: this.userId
          },
          {
            key: 'is_collect',
            rule: '=',
            value: 0
          }
        ]
      }
      const { data } = await getPostVoList(searchParam)
      this.postList = data.list.filter(value => {
        return value.isCollect === 0
      })
      this.loading = false
    },
    async handleLike(postInfo) {
      const postfeedback = {
        postId: postInfo.id,
        isLike: 0
      }
      if (postInfo.isLike === null && postInfo.isCollect === null) {
        await savePostFeedback(postfeedback)
        this.getPostVoList()
      } else {
        await updatePostFeedback(postfeedback)
        this.getPostVoList()
      }
    },
    async handleDislike(postInfo) {
      const postfeedback = {
        postId: postInfo.id,
        isLike: 1
      }
      if (postInfo.isLike === null && postInfo.isCollect === null) {
        await savePostFeedback(postfeedback)
        this.getPostVoList()
      } else {
        await updatePostFeedback(postfeedback)
        this.getPostVoList()
      }
    },
    async handleCollect(postInfo) {
      const postfeedback = {
        postId: postInfo.id,
        isCollect: postInfo.isCollect === 0 ? 1 : 0
      }
      if (postInfo.isLike === null && postInfo.isCollect === null) {
        await savePostFeedback(postfeedback)
        this.getPostVoList()
      } else {
        await updatePostFeedback(postfeedback)
        this.getPostVoList()
      }
    },
    handleReport(postInfo) {
      this.$router.push({
        path: '/report',
        query: {
          postId: postInfo.id
        }
      })
    },
    goPostDetail(item) {
      if (item.status === 1) {
        this.$router.push({
          path: '/postDetail',
          query: {
            postId: item.id
          }
        })
      } else {
        this.$message({
          message: '该帖子已被管理员封禁,请联系管理员解封',
          type: 'warning'
        })
      }
    }
  }
}
</script>

<style lang="scss">
.myCollects {
  .el-empty__description p {
    color: black;
    font-weight: bold;
  }
  .el-loading-mask {
    z-index: 1;
    border-radius: 15px;
    background-image: radial-gradient(white, rgb(212 230 255));
  }
}
</style>

<style lang="scss" scoped>
.myCollects {
  padding: 10px;
    margin: 10px auto;
    border-radius: 15px;
    width: 1150px;
    min-width: 1150px;
    background: rgba(133, 185, 254, 0.738);
    box-shadow: 0 0 6px black;
  .post {
      margin: 10px auto;
      &:hover {
        cursor: pointer;
        outline: 5px solid rgb(36, 36, 36);
      }
    }
}
</style>
